<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="700px">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :search-tags="searchTags"
      :select-list="selectList"
      :rules="rules"
      :is-inline="false"
    />
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        type="primary"
        icon="el-icon-receiving"
        @click="handleSave"
      >{{ $t('save') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAdd, doUpdate, getInfo } from '@/api/configSetting/toolingEqpBind'
import { getEqpTypeList } from '@/api/production/layoutDesign'
import { getToolingTypeIdList } from '@/api/Tooling/toolingType'
export default {
  name: 'ToolingEqpBindModel',
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: {},
      selectList: {},
      searchTags:
        [
          { label: this.$t('equipmentType'), prop: 'layoutStrucId', tag: 'DynSelect', style: { width: '100%' }},
          { label: this.$t('toolingType'), prop: 'toolingTypeIdList', tag: 'DynSelect', multiple: true, style: { width: '100%' }},
          { label: this.$t('remark'), prop: 'remark', tag: 'DynTextarea', row: 3, show_word_limit: true, maxlength: 200 }
        ],
      rules: {
        layoutStrucId: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('equipmentType')}`, trigger: 'change' }],
        toolingTypeIdList: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('toolingType')}`, trigger: 'change' }]
      }
    }
  },
  methods: {
    handleOpen(row) {
      if (row && row.id) {
        this.title = this.$t('modify')
        getInfo({ id: row.id }).then(res => {
          this.searchForm = res.data
        })
      } else {
        this.title = this.$t('add')
      }
      this.getToolingTypes()
      this.getEquipmentTypes()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    },
    handleSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            // const { equipmentTypeID, materialTypeID, materialCodeID, description, id } = this.searchForm
            doUpdate(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          } else {
            // const { equipmentTypeID, materialTypeID, materialCodeID, description } = this.searchForm
            doAdd(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    // 获取设备类型列表
    async getToolingTypes() {
      const res = await getEqpTypeList()
      this.$set(this.selectList, 'layoutStrucId', res.data)
    },
    // 获取备件类型列表
    async getEquipmentTypes() {
      const res = await getToolingTypeIdList()
      this.$set(this.selectList, 'toolingTypeIdList', res.data)
    }
  }
}
</script>
